﻿//#define NETFX35

/*********************************
 $Author: qinjunit@gmail.com $
 $Modtime: 09-03-06 10:25 $
 $Revision: 9 $
********************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace NUnitTest
{
    /// <summary>
    /// 扩展功能函数
    /// </summary>
    public static class ExtensionUtil
    {
        /// <summary>
        /// 连接数据表数据
        /// </summary>
        /// <param name="tab">数据表数据</param>
        /// <param name="colSeparator">列分隔字符</param>
        /// <param name="rowSeparator">行分隔字符</param>
        /// <param name="includeColName">是否包含列名称</param>
        /// <returns></returns>
        public static string Join(
#if NETFX35 
        this 
#endif
DataTable tab, string colSeparator, string rowSeparator, bool includeColName)
        {
            StringBuilder sb = new StringBuilder();
            int iflag = 0;
            if (includeColName)
            {
                foreach(DataColumn col in tab.Columns)
                {
                    sb.AppendFormat("{1}{0}", col.ColumnName, iflag > 0 ? colSeparator : "");
                    iflag++;
                }
                iflag = 1;
            }

            foreach(DataRow dRow in tab.Rows)
            {
                if (iflag > 0) sb.Append(rowSeparator);
                int iRowFlag = 0;
                foreach(DataColumn col in tab.Columns)
                {
                    sb.AppendFormat("{1}{0}", dRow[col], iRowFlag > 0 ? colSeparator : "");
                    iRowFlag++;
                }
                iflag++;
            }
            return sb.ToString();
        }

        /// <summary>
        /// 连接数组项为字符串
        /// </summary>
        /// <param name="arr">数组实例</param>
        /// <param name="itemSeparator">数组项分隔符号</param>
        /// <param name="convert">特许类型转换委托</param>
        /// <returns></returns>
        public static string Join(
#if NETFX35 
        this 
#endif
Array arr, string itemSeparator)
        {
            return Join(arr, itemSeparator, null);
        }

        /// <summary>
        /// 连接数组项为字符串
        /// </summary>
        /// <param name="arr">数组实例</param>
        /// <param name="itemSeparator">数组项分隔符号</param>
        /// <param name="convert">特许类型转换委托</param>
        /// <returns></returns>
        public static string Join(
#if NETFX35 
        this 
#endif
Array arr, string itemSeparator, Converter<object, string> convert)
        {
            StringBuilder sb = new StringBuilder();
            for (long i = 0; i < arr.LongLength; i++)
            {
                if (i != 0) sb.Append(itemSeparator);
                if (convert != null)
                {
                    sb.Append(convert(arr.GetValue(i)));
                }
                else
                {
                    sb.Append(arr.GetValue(i));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取堆栈中最底层触发的异常
        /// </summary>
        /// <param name="exp">当前异常</param>
        /// <returns></returns>
        public static Exception GetTriggerException(
#if NETFX35 
        this 
#endif
Exception exp)
        {
            while (exp.InnerException != null)
            {
                exp = exp.InnerException;
            }
            return exp;
        }
    }

}